<?php
namespace Home\Controller;

class ParentController extends \Common\Controller\BaseController {

    protected function _initialize() {
        $this->checkLanguage();
        $this->switchTimeUtc();
    }

    /**
     * 校验是否已经登录
     * @modify trainyao 2016-04-27    创建
     */
    protected function isLogin() {
        if (is_null(session(C('USER_ID')))) {
            return false;
        }
        return true;
    }

    protected function logout() {
        session(C('USER_ID'), null);
        session(C('USER_NAME'), null);
        session(C('USER_AREACODE'), null);
        session(C('USER_MOBILE'), null);
        session(C('WECHAT_OAUTH_LOGIN'), null);
        session(C('WECHAT_LOGIN_OPEN_ID'), null);
        session(C('AUTH_LOGIN_TYPE'), null);
    }

    /**
     * 根据语言切换web页面语言
     * @modify trainyao 2016-02-24    创建
     */
    protected function checkLanguage() {
        if (!in_array(LANG_SET, array('zh_cn', 'en_us', 'zh_tw'))) {
            $this->theme('en_us');
        } else {
            $this->theme(LANG_SET);
        }
    }

    /**
     * 言切时区
     * @modify wenhui 2017-06-22    创建
     */
    protected function switchTimeUtc() {
        $hotel = I('param.hotel');
        // 客栈时区
        $hotelEntityModel = D('HotelEntity')->getInfo($hotel);
        //获取区号时区数据
        $UserLogic = A('Common/User', 'Logic');
        $universalTimeData = $UserLogic->universalTimeList();
        $universalTimeData = getArrayValueAsIndex($universalTimeData, 'universalTimeId');
        $universalTimeEtc = $universalTimeData[$hotelEntityModel['universaltime_id']]['universalTimeEtc'];
        $universalTimeEtc = isset($universalTimeEtc) && !is_null($universalTimeEtc) ? $universalTimeEtc : 'Etc/GMT-8';
        date_default_timezone_set($universalTimeEtc);
    }

    /**
     * 是否在下单分割时间段之内
     * @modify wenhui 2017-11-06    创建
     */
    protected function isSplitOrderTime($hotel, $startDate) {
        $this->switchTimeUtc();
       // 是否在下单分割时间段之内
        $currentHour = date('H:i');
        $hotelInfo = D('HotelInfo')->getInfoByHotel($hotel);
        $hotelOrderDiffTime = isset($hotelInfo['splitordertime']) ? $hotelInfo['splitordertime'] : '00:00';
        if (date('Y-m-d') == $startDate && '00:00' < $currentHour && $currentHour < $hotelOrderDiffTime) {
            return true;
        }
        return false;
    }


    /**
     * 赋值 | 获取 客栈默认货币
     * @modify wenhui 2018-02-05    创建
     */
    protected function assignHotelDefultCurrency($hotel, $getCurrency = false) {
        $currencyModel = D('Currency');
        $currencyHotelModel = D('CurrencyHotel');

        $bpCurrency = $currencyModel->getCurrencyInfo();
        $bpCurrency = getArrayValueAsIndex($bpCurrency, 'id');

        $defaultHotelCurrency = $currencyHotelModel->getDefaultCurrency($hotel);
        $defaultSymbol = isset($bpCurrency[$defaultHotelCurrency['currency_id']]['symbol']) ? $bpCurrency[$defaultHotelCurrency['currency_id']]['symbol'] : '¥';
        if ($getCurrency) {
            return $defaultHotelCurrency;
        }

        $this->assign('defaultSymbol', $defaultSymbol);
        return;
    }
}